package com.coderyeah.cosmetology.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.coderyeah.cosmetology.entity.Technician;
import com.coderyeah.cosmetology.mapper.TechnicianMapper;
import com.coderyeah.cosmetology.service.TechnicianService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coderyeah.cosmetology.util.Result;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lqs
 * @since 2022-05-24
 */
@Service
public class TechnicianServiceImpl extends ServiceImpl<TechnicianMapper, Technician> implements TechnicianService {
    @Resource
    private TechnicianMapper technicianMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result queryAllTechInfo() {
        String key = "CACHE:TECHS";
        String s = stringRedisTemplate.opsForValue().get(key);
        if (StrUtil.isNotBlank(s)) {
            List<Technician> technicians = JSONUtil.toList(s, Technician.class);
            return Result.success(technicians);
        }

        List<Technician> technicians = technicianMapper.selectList(new QueryWrapper<Technician>());
        stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(technicians), 30, TimeUnit.MINUTES);
        return Result.success(technicians);
    }

    @Override
    public Result queryTechInfo(String id) {
        Technician technician = technicianMapper.selectById(id);
        return Result.success(technician);
    }
}
